package client;

import common.Message;
import common.MessageType;
import common.User;

import java.io.*;
import java.lang.invoke.VarHandle;
import java.net.InetAddress;
import java.net.Socket;
import java.util.HashMap;

//用户验证
public class UserClientService {

    User user = new User();
    Socket socket;

    public boolean userAuthentication(String userId,String passwd)
    {
        boolean b = false;
        user.setUserId(userId);
        user.setPasswd(passwd);
        //创建验证用户名信息，如果成功，返回true，然后用线程保持链接状态
        try {
            //发送对象流请求
            Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            objectOutputStream.writeObject(user);
            //收到结果
            ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
            Message message = (Message)objectInputStream.readObject();
            //登录成功
            if (message.getMesType().equals(MessageType.MESSAGE_LOGIN_SUCCEED))
            {
                //成功后要保存这个stock到一个线程去持续使用
                ClientConnectServiceThread clientConnectServiceThread = new ClientConnectServiceThread(socket);
                clientConnectServiceThread.start();
                //存放到一个集合中进行管理。因为可能一边聊天，一边传文件，有多个线程同时
                ManageClientConnectServiceThread.addThreadMap(userId,clientConnectServiceThread);
                System.out.println("当前连接数量："+ManageClientConnectServiceThread.clientConnectServiceThreadMap.size());
                b = true;
            }
            else
            {
                //账号密码错误了关闭链接
                socket.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return b;
    }

    public void getOnline(String userId)
    {
        Message message = new Message();
        message.setMesType(MessageType.MESSAGE_GET_ONLINE_FRIEND);
        message.setSender(userId);
        //从集合中获得当前用户的soket
        ClientConnectServiceThread threadMap = ManageClientConnectServiceThread.getThreadMap(userId);
        Socket socket = threadMap.getSocket();
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            objectOutputStream.writeObject(message);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
    //退出
    public void loginOut(String userId)
    {
        Message message = new Message();
        message.setMesType(MessageType.MESSAGE_CLIENT_EXIT);
        message.setSender(userId);
        //从集合中获得当前用户的soket
        ClientConnectServiceThread threadMap = ManageClientConnectServiceThread.getThreadMap(userId);
        Socket socket = threadMap.getSocket();
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            objectOutputStream.writeObject(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //发送私聊信息
    public void privateChat(String userId,String targetUserId,String mes)
    {
        Message message = new Message();
        message.setSender(userId);
        message.setGetter(targetUserId);
        message.setContent(mes);
        message.setMesType(MessageType.MESSAGE_COMM_MES);
        //从集合中获得当前用户的soket
        ClientConnectServiceThread threadMap = ManageClientConnectServiceThread.getThreadMap(userId);
        Socket socket = threadMap.getSocket();
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            objectOutputStream.writeObject(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //群发消息
    public void groupChat(String userId,String mes) {

        Message message = new Message();
        message.setSender(userId);
        message.setContent(mes);
        message.setMesType(MessageType.MESSAGE_COMM_MES);
        //从集合中获得当前用户的soket
        ClientConnectServiceThread threadMap = ManageClientConnectServiceThread.getThreadMap(userId);
        Socket socket = threadMap.getSocket();
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            objectOutputStream.writeObject(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sendFile(String userId,String target,String url)
    {

        FileInputStream fileInputStream = null;
        try {
            //创建和文件大小一样的字节数组，这样只要读一次
            File file = new File(url);
            byte[] bytes = new byte[(int)file.length()];
             fileInputStream = new FileInputStream(file);
             fileInputStream.read(bytes);

            Message message = new Message();
            message.setSender(userId);
            message.setGetter(target);
            message.setFileByte(bytes);
            message.setMesType(MessageType.MESSAGE_FILE);
            //从集合中获得当前用户的soket
            ClientConnectServiceThread threadMap = ManageClientConnectServiceThread.getThreadMap(userId);
            Socket socket = threadMap.getSocket();
            try {
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                objectOutputStream.writeObject(message);
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
